package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.mapper.ApUserMapper;
import com.zhentao.model.common.app.dto.AgistDto;
import com.zhentao.model.common.app.dto.LoginDto;
import com.zhentao.model.common.app.dto.UserDto;
import com.zhentao.model.common.app.pojo.ApUser;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApUserService;
import com.zhentao.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 86150
 */
@Service
public class ApUserServiceImpl implements ApUserService {
@Resource
private ApUserMapper userMapper;
    @Override
    public ResponseResult login(LoginDto loginDto) {
        if(StringUtils.isBlank(loginDto.getPhone()) || StringUtils.isBlank(loginDto.getPassword())){
            return   ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        QueryWrapper<ApUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUser::getPhone,loginDto.getPhone());
        ApUser apUser = userMapper.selectOne(queryWrapper);
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        String password = apUser.getPassword();
        String salt = apUser.getSalt();
        String newpas = DigestUtils.md5DigestAsHex((loginDto.getPassword() + salt).getBytes());
        if(! newpas.equals(password)){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        Map<String ,Object> map=new HashMap<>();
        String token = AppJwtUtil.getToken(apUser.getId().longValue());
        map.put("token",token);
        apUser.setSalt("");
        apUser.setPassword("");
        map.put("user",apUser);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult regist(AgistDto agistDto) {
        if(StringUtils.isBlank(agistDto.getPhone())||StringUtils.isBlank(agistDto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"用户名或密码不能为空");
        }else {
            QueryWrapper<ApUser> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(ApUser::getPhone,agistDto.getPhone());
            ApUser apUser = userMapper.selectOne(queryWrapper);
            System.out.println("手机号查询到的详细信息 = " + apUser);
            if(apUser != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"此用户已存在");
            }else {
                ApUser apUser1 = new ApUser();
                BeanUtils.copyProperties(agistDto,apUser1);

//                给密码加盐
                String Salt ="123";
                String pwd = apUser1.getPassword();
                pwd= DigestUtils.md5DigestAsHex((pwd+Salt).getBytes());
                apUser1.setPassword(pwd);
                apUser1.setSalt(Salt);
                apUser1.setCreateTime(new Date());
//                新增用户
                userMapper.insert(apUser1);
                return ResponseResult.okResult(apUser1);
            }
        }
    }

    @Override
    public ResponseResult findById(Integer id) {
        ApUser apUser = userMapper.selectById(id);
        return ResponseResult.okResult(apUser);
    }

    @Override
    public ResponseResult update1(ApUser apUser) {
        apUser.setUpdateTime(new Date());
        apUser.setCreateTime(new Date());
        int i = userMapper.updateById(apUser);
        if(i==1){
            return ResponseResult.okResult("修改成功");
        }else {
            return ResponseResult.errorResult(402,"修改失败");
        }

    }

    @Override
    public ResponseResult findByName(UserDto userDto) {
        userDto.checkParam();
        LambdaQueryWrapper<ApUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ApUser::getName,userDto.getName());
        Page<ApUser> page = new Page<>(userDto.getPage(),userDto.getSize());
        page = userMapper.selectPage(page, wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

}
